package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.condiction.EqGameInfoCondiction;
import com.poobo.dao.IEqGamesInfoDao;
import com.poobo.entity.TbEqGamesInfo;
import com.poobo.util.pager.Page;

@Repository
public class EqGamesInfoDaoImpl extends BaseDaoImpl implements IEqGamesInfoDao{

	@Override
	public List<TbEqGamesInfo> queryAll() {
		String hql = " from TbEqGamesInfo ";
		return (List<TbEqGamesInfo>) findAll(hql);
	}

	@Override
	public TbEqGamesInfo queryEqGamesInfoById(Integer id) {
		return selectObjectById(TbEqGamesInfo.class, id);
	}

	@Override
	public Page queryEqGamesInfoPage2(TbEqGamesInfo eqGamesInfo, Page page) {
		String hql = " from TbEqGamesInfo where 1=1 ";
		String countHql = " select count(*) from TbEqGamesInfo where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		String orderBy = " order by id desc";
		StringBuffer where = new StringBuffer();
		if( eqGamesInfo != null ){
			if( eqGamesInfo.getEqid() != null ){
				where.append(" and eqid = ? ");
				params.add(eqGamesInfo.getEqid());
			}
			if( eqGamesInfo.getReid() != null ){
				where.append(" and reid = ? ");
				params.add(eqGamesInfo.getReid());
			}
		}
		page.setBaseHql(hql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		return getPageByHql(page);
	}

	@Override
	public void deleteEqGamesInfoById(Integer id) {
		String hql = " delete from TbEqGamesInfo where id = "+ id +" ";
		delObjectByIds(hql);
	}

	@Override
	public void saveEqGamesInfo(TbEqGamesInfo eqGamesInfo) {
		saveOrUpdate(eqGamesInfo);
	}

	@Override
	public Page queryEqGamesInfoPage(EqGameInfoCondiction condiction,
			Page page) {
		String baseSql = "select a.ID as id,a.eqid,a.reid,a.isDefault,b.number,b.shopId,b.createDate,c.`NAME` as name " +
				"from tb_eq_games_info a " +
				"left join tb_equipment b on a.eqid=b.ID " +
				"left join tb_resources c on a.reid=c.ID " +
				"where 1=1 ";
		String countSql = "select count(*) from tb_eq_games_info a " +
				"left join tb_equipment b on a.eqid=b.ID " +
				"left join tb_resources c on a.reid=c.ID " +
				"where 1=1 ";
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		String groupBy = " group by a.ID ";
		String orderBy = " order by a.isDefault desc,a.ID desc ";
		if ( condiction.getEquipment() != null && condiction.getEquipment().getNumber() != null && condiction.getEquipment().getNumber() != "") {
			where.append(" and b.number like '%" + condiction.getEquipment().getNumber() + "%' ");
		}
		if ( condiction.getEqid() != null ) {
			where.append(" and a.eqid =" + condiction.getEqid() + " ");
		}
		page.setBaseSql(baseSql + where.toString() + groupBy + orderBy);
		page.setCountSql(countSql + where.toString() + groupBy);
		page.setParam(params);
		Page page2 = getPageBySql(page);
		page2.setPageList(getMapBySql(baseSql + where.toString() + groupBy + orderBy, null));
		return page2;
	}

	@Override
	public void updateIsDefault( Integer eqid ) {
		String sql = " update tb_eq_games_info set isDefault=0 where eqid="+eqid+" ";
		execSql(sql);
	}

	@Override
	public void deleteEqGamesInfoByEquipmentId(Integer eqid) {
		String sql = " delete from tb_eq_games_info where eqid="+eqid+" ";
		execSql(sql);
	}

	@Override
	public void deleteEqGamesInfoByResourcesId(Integer reid) {
		String sql = " delete from tb_eq_games_info where reid="+reid+" ";
		execSql(sql);
	}

	@Override
	public TbEqGamesInfo queryEqGamesInfoByEquipmentId(Integer id) {
		String hql = "from TbEqGamesInfo where eqid="+id+" and isDefault=1 ";
		log.info("hql="+hql);
		List<TbEqGamesInfo> list = (List<TbEqGamesInfo>)findAll(hql);
		if (list.isEmpty()) {
			log.info("设备游戏信息没找到");
			return null;
		}
		log.info("设备游戏信息id="+list.get(0).getId());
		return list.get(0);
	}

	@Override
	public List<TbEqGamesInfo> findDefaultGame() {
		String hql = " from TbEqGamesInfo where isDefault=1 ";
		return (List<TbEqGamesInfo>) findAll(hql);
	}
}
